package com.khk.raidCalculator;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;

import com.khk.raidCalculator.R;

public class RaidCalculator extends Activity{
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
	}
	
	public void compute(View button) {
		final Spinner spnDiskSize = (Spinner) findViewById(R.id.spnDiskSize);
		String disksize = spnDiskSize.getSelectedItem().toString();
		
		final Spinner spnRaidType = (Spinner) findViewById(R.id.spnRaidType);
		String raidtype = spnRaidType.getSelectedItem().toString();
		
		final EditText txtNumDisk = (EditText) findViewById(R.id.txtNumDisk);
		String numdiskStr = txtNumDisk.getText().toString();
		
		int numdisk;
		try {
			numdisk = Integer.parseInt(numdiskStr);
			int size = 0;
			
			if (disksize == getString(R.string.disk450G)) {
				size = 450;
			} else if (disksize == getString(R.string.disk600G)) {
				size = 600;
			} else if (disksize == getString(R.string.disk1T)) {
				size = 1000;
			} else if (disksize == getString(R.string.disk300G)) {
				size = 300;
			} else if (disksize == getString(R.string.disk73G)) {
				size = 73;
			} else if (disksize == getString(R.string.disk100G)) {
				size = 100;
			} else if (disksize == getString(R.string.disk200G)) {
				size = 200;
			} else if (disksize == getString(R.string.disk400G)) {
				size = 400;
			} else if (disksize == getString(R.string.disk500G)) {
				size = 500;
			} else if (disksize == getString(R.string.disk2T)) {
				size = 2000;
			}
			
			if (raidtype == getString(R.string.raid5)) {
				computeRAID5(size, numdisk);
			} else if (raidtype == getString(R.string.raid10)) {
				computeRAID10(size, numdisk);
			} else if (raidtype == getString(R.string.raid1)) {
				computeRAID1(size, numdisk);
			} else if (raidtype == getString(R.string.raid6)) {
				computeRAID6(size, numdisk);
			} else if (raidtype == getString(R.string.raid0)) {
				computeRAID0(size, numdisk);
			}
			
		} catch (NumberFormatException e) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("Invalid value for number of disks")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
		}
	}

	private void computeRAID0(int size, int numdisk) {
		showResult(size * numdisk * 0.9);
	}

	private void computeRAID6(int size, int numdisk) {
		if (numdisk < 4) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("RAID 6 requires at least 4 disks.")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
			return;
		}
		
		showResult(size * (numdisk - 2) * 0.9);
	}

	private void computeRAID1(int size, int numdisk) {
		if (numdisk < 2) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("RAID 1 requires at least 2 disks.")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
			return;
		} else if (numdisk % 2 != 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("RAID 1 requires even number of disks.")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
			return;
		}
		
		showResult(size * (numdisk / 2) * 0.9);
	}

	private void computeRAID10(int size, int numdisk) {
		if (numdisk < 4) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("RAID 10 requires at least 4 disks.")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
			return;
		} else if (numdisk % 2 != 0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("RAID 10 requires even number of disks.")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
			return;
		}
		
		showResult(size * (numdisk / 2) * 0.9);
	}

	private void computeRAID5(int size, int numdisk) {
		if (numdisk < 3) {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage("RAID 5 requires at least 3 disks.")
				.setPositiveButton(getString(R.string.ok), null);
			AlertDialog alert = builder.create();
			alert.show();
			return;
		}
		
		showResult(size * (numdisk - 1) * 0.9);
	}

	private void showResult(double d) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Estimated usable size:\n" + d + " GB")
			.setPositiveButton(getString(R.string.ok), null);
		AlertDialog alert = builder.create();
		alert.show();
	}

}
